Rewrite xml-insert.sh in portable bourne-shell
authorMartin Nordholts <martinn@src.gnome.org>
Tue, 31 Mar 2009 17:09:34 +0000 (17:09 +0000)
committerMartin Nordholts <martinn@src.gnome.org>
Tue, 31 Mar 2009 17:09:34 +0000 (17:09 +0000)
Patch from Gary V. Vaughan

svn path=/trunk/; revision=406

ChangeLog
docs/Makefile.am
docs/tools/xml_insert.sh

index b1e555d19ef3b5a6dcd17595dce98f35d5d6ade2..08278c58aa3423d6ba02b0f59a7261c078a50ec3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-03-25  Martin Nordholts  <martinn@svn.gnome.org>
+
+       Patch from Gary V. Vaughan. Rewrite xml-insert.sh in portable
+       bourne-shell
+
+       * docs/Makefile.am
+       * docs/tools/xml_insert.sh
+
 2009-03-25  Martin Nordholts  <martinn@svn.gnome.org>
 
        Patch from Gary V. Vaughan. Don't use depecrated features or
index 773d1ab227f7b30fec5c47dc46d53d7cec6c44b1..aa617a0d74f5dcbce8251963865c8f0d65b7a31f 100644 (file)
@@ -54,16 +54,16 @@ index.html: index-static.html                    \
            Makefile.am
        echo -n "HTML: $@"
        cp $< $@ 
-       (which mktemp > /dev/null&& TMPFILE=`mktemp` || TMPFILE="/tmp/babl_build_tempfile" ;\
+       (which mktemp > /dev/null 2>&1 && TMPFILE=`mktemp` || TMPFILE="/tmp/babl_build_tempfile" ;\
        export BABL_PATH="$(top_builddir)/extensions"; $(babl_html_dump) > $$TMPFILE;\
-       $(top_srcdir)/docs/tools/xml_insert.sh $@ BablBase $$TMPFILE;\
+       $(SHELL) $(top_srcdir)/docs/tools/xml_insert.sh $@ BablBase $$TMPFILE;\
        rm -f $$TMPFILE )
        echo -n "."
 
-       $(top_srcdir)/docs/tools/xml_insert.sh $@ BablFishPath BablFishPath.txt
-       $(top_srcdir)/docs/tools/xml_insert.sh $@ AUTHORS $(top_srcdir)/AUTHORS
-       $(top_srcdir)/docs/tools/xml_insert.sh $@ TODO $(top_srcdir)/TODO
-       $(top_srcdir)/docs/tools/xml_insert.sh $@ NEWS $(top_srcdir)/NEWS
+       $(SHELL) $(top_srcdir)/docs/tools/xml_insert.sh $@ BablFishPath BablFishPath.txt
+       $(SHELL) $(top_srcdir)/docs/tools/xml_insert.sh $@ AUTHORS $(top_srcdir)/AUTHORS
+       $(SHELL) $(top_srcdir)/docs/tools/xml_insert.sh $@ TODO $(top_srcdir)/TODO
+       $(SHELL) $(top_srcdir)/docs/tools/xml_insert.sh $@ NEWS $(top_srcdir)/NEWS
        echo " [OK]"
 
 distclean-local:
index 139bd15066cb17214c49c30ae366b2cca2914c4c..16b6b246d69e4f3a0d0416261f81c421f6844b44 100755 (executable)
 #
 # FIXME: add argument checking / error handling
 
-which tempfile > /dev/null && TMP_FILE=`tempfile` || TMP_FILE="/tmp/temp_file"
+: ${AWK="awk"}
+: ${ECHO="echo"}
+: ${MKDIR="mkdir"}
+: ${SED="sed"}
+: ${Xsed="$SED -e 1s/^X//"}
 
-cp $1 $TMP_FILE
+# Global variables:
+EXIT_SUCCESS=0
+EXIT_FAILURE=1
 
-SPLIT=`grep -n "<\!--$2-->" $TMP_FILE|head -n 1|sed -e "s/:.*//"`;
-head -n $SPLIT $TMP_FILE > $1
+dirname="s,/[[^/]]*$,,"
+basename="s,^.*/,,"
+
+# Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh
+# is ksh but when the shell is invoked as "sh" and the current value of
+# the _XPG environment variable is not equal to 1 (one), the special
+# positional parameter $0, within a function call, is the name of the
+# function.
+progpath="$0"
+
+# The name of this program:
+# In the unlikely event $progname began with a '-', it would play havoc with
+# func_echo (imagine progname=-n), so we prepend ./ in that case:
+progname=`$ECHO "X$progpath" | $Xsed -e "$basename" -e 's,^-,./-,'`
+
+# func_error arg...
+# Echo program name prefixed message to standard error.
+func_error ()
+{
+    $ECHO "$progname: "${1+"$@"} 1>&2
+}
+
+# func_fatal_error arg...
+# Echo program name prefixed message to standard error, and exit.
+func_fatal_error ()
+{
+    func_error ${1+"$@"}
+    exit $EXIT_FAILURE
+}
+
+# func_mktempdir [string]
+# Make a temporary directory that won't clash with other running
+# processes, and avoids race conditions if possible.  If
+# given, STRING is the basename for that directory.
+func_mktempdir ()
+{
+    my_template="${TMPDIR-/tmp}/${1-$progname}"
+
+    if test "$opt_dry_run" = ":"; then
+      # Return a directory name, but don't create it in dry-run mode
+      my_tmpdir="${my_template}-$$"
+    else
+
+      # If mktemp works, use that first and foremost
+      my_tmpdir=`mktemp -d "${my_template}-XXXXXXXX" 2>/dev/null`
+
+      if test ! -d "$my_tmpdir"; then
+        # Failing that, at least try and use $RANDOM to avoid a race
+        my_tmpdir="${my_template}-${RANDOM-0}$$"
+
+        save_mktempdir_umask=`umask`
+        umask 0077
+        $MKDIR "$my_tmpdir"
+        umask $save_mktempdir_umask
+      fi
+
+      # If we're not in dry-run mode, bomb out on failure
+      test -d "$my_tmpdir" || \
+        func_fatal_error "cannot create temporary directory \`$my_tmpdir'"
+    fi
+
+    $ECHO "X$my_tmpdir" | $Xsed
+}
+
+tmp_dir="`func_mktempdir`"
+tmp_file="$tmp_dir/one"
+
+cp $1 $tmp_file
+
+numlines=`wc -l $tmp_file | $AWK '{print $1;}'`
+splitno=`$AWK "/<\!--$2-->/ { print NR; exit 0; }" $tmp_file`
+tailno=`expr $numlines - $splitno`
+
+head -$splitno $tmp_file > $1
 cat $3 >> $1
-tail -n $((`wc -l $TMP_FILE | sed -e "s/ .*//"` - $SPLIT )) $TMP_FILE >> $1
+tail -$tailno $tmp_file >> $1
+
+rm -rf $tmp_dir
 
-rm $TMP_FILE
+exit $?